package com.anycl.customer.controller;

import com.anycl.customer.constant.Constants;
import com.anycl.customer.exception.CustomerException;
import com.anycl.customer.service.AdminUserService;
import com.anycl.customer.vo.AdminUserVo;
import com.anycl.customer.vo.LoginVo;
import com.anycl.customer.vo.RegisterVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("/user")
public class AdminUserController {

    private final AdminUserService adminUserService;

    public AdminUserController(AdminUserService adminUserService) {
        this.adminUserService = adminUserService;
    }

    @PostMapping("/login")
        public AdminUserVo login(@RequestBody LoginVo loginVo, HttpSession httpSession) throws CustomerException {
        AdminUserVo adminUserVo =  adminUserService.login(loginVo);
        if (adminUserVo.getStatus().equals(Constants.SuccessMsg.SUCCESS_STATUS)) {
            httpSession.setAttribute(Constants.CURRENT_USER, adminUserVo);
        }
        return adminUserVo;
    }

    @PostMapping("/signIn")
    public AdminUserVo signIn(@RequestBody RegisterVo registerVo) throws CustomerException {
        log.info("registerVo  " + registerVo);
        LoginVo loginVo = registerVo;
        return adminUserService.signIn(loginVo, registerVo.getAuthCode());
    }
}
